/*
 * Copyright (c) 2008-2021 浩瀚深度 All Rights Reserved.
 *
 * <p>FileName: DataSourceController.java</p>
 *
 * @author zz
 * @date 2024/2/4
 * @version 1.0
 * History:
 * v1.0.0 2024/2/4 Create
 */
package com.sqlweb.sqlweb.controller;

import com.sqlweb.sqlweb.base.Result;
import com.sqlweb.sqlweb.base.ResultCode;
import com.sqlweb.sqlweb.config.DatabaseConfig;
import com.sqlweb.sqlweb.entity.DataSourceConfig;
import com.sqlweb.sqlweb.entity.MonitoringData;
import com.sqlweb.sqlweb.mapper.SqlExecuteLogMapper;
import com.sqlweb.sqlweb.service.DataSourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>Title: DataSourceController</p>
 * <p>Description: </p>
 *
 * @author zz
 */
@Slf4j
@RestController
@RequestMapping("/datasource")
@CrossOrigin(origins = "*")
public class DataSourceController {
    @Autowired
    private DataSourceService dataSourceService;

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private DatabaseConfig databaseConfig;
    @Autowired
    SqlExecuteLogMapper sqlExecuteLogMapper;

    @PostMapping("/add")
    public Result addDataSource(@RequestBody DataSourceConfig dataSourceConfig) {
        dataSourceService.addDataSourceConfig(dataSourceConfig);
        return new Result(ResultCode.SUCCESS, "添加成功");
    }

    @PostMapping("/remove")
    public Result removeDataSource(@RequestBody DataSourceConfig dataSourceConfig) {
        dataSourceService.removeDataSourceConfig(dataSourceConfig);
        return new Result(ResultCode.SUCCESS, "删除");
    }

    @PostMapping("/list")
    public Result listDataSource() {
        List<DataSourceConfig> dataSourceConfigs = dataSourceService.selectAll();
        return new Result(ResultCode.SUCCESS, dataSourceConfigs);
    }

    @PostMapping("/dbTypes")
    public List<DatabaseConfig.DbType> getDbTypes() {
        return databaseConfig.getDbType();
    }

    @GetMapping("/monitoring")
    public ResponseEntity<?> getMonitoringData() {
        // 创建一个响应对象，包含监控数据
        MonitoringData monitoringData = new MonitoringData();
        monitoringData.setMaxConnections(999);
        monitoringData.setCurrentConnections(90);
        monitoringData.setTotalData(1024);
        monitoringData.setTableCount(34);
        //monitoringData.setTableDataDistribution(/* 某种方式获取表数据分布 */);
        return ResponseEntity.ok(monitoringData);
    }

}

